package com.sfx.messagequeue.mqServer.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sfx.messagequeue.common.consumer.ConsumerEnv;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-30
 * Time: 18:03
 */
@Slf4j
@Data
@NoArgsConstructor
public class MSGQueue {
    //队列的唯一身份标识
    private String name;
    //队列是否支持持久化 true:支持 false:不支持
    private boolean durable = false;
    //队列是否独占,为true:只能被一个消费者使用,其他消费者不能使用 false:所有消费者都能使用
    private boolean exclusive = false;
    //队列是否支持自动删除 true:支持 false:不支持
    private boolean autoDelete = false;
    //队列的一些额外参数选项
    private Map<String, Object> arguments = new HashMap<>();
    //表示消息的订阅者
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();

    //表示 消费者轮询的下标
    private AtomicInteger consumerEq = new AtomicInteger(0);

    //添加消息的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv) {
        consumerEnvList.add(consumerEnv);
    }

    // 采用轮询的方式来从订阅者中挑选出消息的消费者
    public ConsumerEnv chooseConsumerEnv() {
        // 表示没有人订阅
        if (consumerEnvList.size() == 0) {
            return null;
        }
        int index = consumerEq.get() % consumerEnvList.size();
        consumerEq.getAndDecrement();
        return consumerEnvList.get(index);
    }

    public MSGQueue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) {
        this.name = name;
        this.durable = durable;
        this.exclusive = exclusive;
        this.autoDelete = autoDelete;
        this.arguments = arguments;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String arguments = objectMapper.writeValueAsString(this.arguments);
            return arguments;
        } catch (JsonProcessingException e) {
            log.error("arguments转Map错误 {}", e);
            e.printStackTrace();
        }
        //抛出异常返回空的字符串
        return "{}";
    }

    public void setArguments(String arguments) {
        //转换成arguments-->Map类型
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //将String类型转换成arguments
            Map<String, Object> readValue = objectMapper.readValue(arguments, new TypeReference<Map<String, Object>>() {
            });
            this.arguments = readValue;
        } catch (JsonProcessingException e) {
            log.error("arguments转Map错误 {}", e);
            e.printStackTrace();
        }
    }

    /**
     * 用于测试
     *
     * @param key
     * @param val
     */
    public void setArguments(String key, String val) {
        arguments.put(key, val);
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }
}